package cn.edu.scau.cmi.liangzijian.hibernate.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.liangzijian.hibernate.domain.Teacher;
import cn.edu.scau.cmi.liangzijian.hibernate.util.HibernateSessionFactoryUtil;

public class TeacherDAO {
	private static final Logger LOG = LoggerFactory.getLogger("cn.edu.scau.cmi.liangzijian.hibernate.domain.Teacher");
	private static final String NAME = "name";
	private static final String ID = "id";
	
	public void insert(Teacher transientInstance) {
		try {
			Session session = getSession();
			Transaction transaction = session.beginTransaction();
			
			session.save(transientInstance);
			transaction.commit();
			session.close();
			
			LOG.debug("insert successful");
		} catch(RuntimeException re) {
			LOG.error("delete failed", re);
			throw re;
		}
		
	}
	
	public void delete(Teacher persistentInstance) {
		LOG.debug("deleting TEACHER instance");
		try {
			Session session = getSession();
			Transaction transaction = session.beginTransaction();
			
			session.delete(persistentInstance);
			transaction.commit();
			session.close();
			
			LOG.debug("delete successful");
		} catch(RuntimeException re) {
			LOG.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Teacher persistentInstance) {
		LOG.debug("deleting TEACHER instance");
		try {
			Session session = getSession();
			Transaction transaction = session.beginTransaction();
			
			session.update(persistentInstance);
			transaction.commit();
			session.close();
			
			LOG.debug("delete successful");
		} catch(RuntimeException re) {
			LOG.error("delete failed", re);
			throw re;
		}
	}
		
	public Teacher findById(int id) {
		return (Teacher)findByProperty(ID, id).get(0);
	}
	
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}
	
	//hql语句里面，表名为对应的实体类名，列名为对应的属性名
	public List findByProperty(String propertyName, Object value) {
		LOG.debug("finding TEACHER instance with property: " + propertyName + ", value: " + value);
		
		try {
			Session session = getSession();
			
			String queryString = "FROM Teacher WHERE " + propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			
			List results = queryObject.list();
			
			session.close();
			LOG.debug("find by property successful, result size: " + results.size());
			return results;
		} catch(RuntimeException re) {
			LOG.error("find by example failed");
			throw re;
		}
	}
	
	public List findAll() {
		LOG.debug("finding all Students instance");
		try {
			Session session = getSession();
			
			String queryString = "FROM Teacher";
			Query queryObject = session.createQuery(queryString);
			
			List results = queryObject.list();
			
			session.close();
			LOG.debug("find by property successful, result size: " + results.size());
			return results;
		} catch(RuntimeException re) {
			LOG.error("find all failed", re);
			throw re;
		}
	}
	
	public Session getSession() { 
		return HibernateSessionFactoryUtil.getSession();
	}
}
